// pages/scanimg/upload.js
let app=getApp();
let storage = require('../../wechat-lib/storage')
Page({
  mixins: [],
  /**
   * 页面的初始数据
   */
  data: {
    select:0,
    src:"/static/logo.png"
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.imgSrc) {
      this.setData({
        src: app.globalData.imgSrc,
        select: 1
      })
      app.globalData.imgSrc=undefined;
      wx.showToast({
        "title": '点击图片识别',
        "duration": 1000
      })
    }
  },
  ChooseImage(res) {
    let that=this;
    storage.takePicture(res).then(src=>{
      console.log(src)
      storage.checkImage(src,{}).then(check=>{
        console.log(check)
        wx.hideShareMenu({
          menus: ['shareAppMessage', 'shareTimeline']
        })
        if(check==true){
          // wx.showShareMenu({
          //   menus: ['shareAppMessage', 'shareTimeline']
          // })
          this.setData({
            src: src,
            select: 1
          })
        }else{
          wx.hideShareMenu({
            menus: ['shareAppMessage', 'shareTimeline']
          })
          wx.showToast({
            title: '内容中含敏感内容，请修改后提交！',
            icon: "none"
          })
          this.setData({
            src:"/static/logo.png",
            select: 0
          })
        }
        
      },err=>{
        console.error(err)
        wx.showToast({
          title: err,
          icon: "error"
        })
      })
      
    },res=>{
      res && wx.showToast({
        title: res,
        icon: "error"
      })
    })
  },
  DelImg(e) {
    wx.showModal({
      title: '删除',
      content: '确定要删除吗？',
      cancelText: '取消',
      confirmText: '删除',
      success: res => {
        if (res.confirm) {
          this.setData({
            src: '',
            select: 0
          })
          wx.showToast({
            "title": '请上传新图片',
            "duration": 1000
          })
        }
      }
    })
  },
  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function () {
  //   return ;
  //     wx.showModal({
  //       title: '谢谢您的分享',
  //       content: '谢谢你的分享，您用行动支持了我们。希望关注我们其它应用。',
  //     })
  // },
  upload: function () {
    var that = this;
    storage.uploadFile(that.data.src, { }).then(res=>{
      console.log(res)
      wx.navigateTo({
        url: '/pages/scanimg/result?url=' + encodeURIComponent(res.url)+"&id="+encodeURIComponent(res.id)
      })
    })
  },
  cropper:function(){
    let that=this;
    wx.navigateTo({
      url: 'cropperpage?image='+that.data.src,
    })
  },
})